# 使用scrapy模拟完成人人网登陆的登陆操作。网址：http://www.renren.com/
# 第九周作业2)
# 班级：Python五期
# 学员：李子坚

# -*- coding: utf-8 -*-
import scrapy,json


class RenrenloginSpider(scrapy.Spider):
    '''模拟人人网登陆爬虫类'''

    name = 'renrenlogin'
    allowed_domains = ['www.renren.com']
    #start_urls = ['http://www.renren.com/']

    def start_requests(self):
        '''模拟人人网登陆操作'''
        #设置模拟人人网登陆提交链接
        url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2018642047483'

        #设置模拟人人网登陆参数
        data = {
            'email':'13922870074',
            'icode':'',
            'origURL':'http://www.renren.com/home',
            'domain':'renren.com',
            'key_id':'1',
            'captcha_type':'web_login',
            'password':'67031dbdbf9bc7380d4772a6e28085bc733f71c80f6488d8e49258ffb88666dc',
            'rkey':'cb15f985754fd884a44506ff5db1256e',
            'f':'http%253A%252F%252Fzhibo.renren.com%252Ftop',
        }
        #Scrapy发送模拟人人网登陆POST请求
        yield scrapy.FormRequest(
            url = url,
            formdata = data,
            callback = self.after_login
        )

    def after_login(self, response):
        '''模拟人人网登陆后打开个人主页'''
        if response.status == 200:
            print("模拟人人网登录成功！")
            myurl = 'http://www.renren.com/966800015/profile'
            #Scrapy发送模拟访问人人网个人主页GET请求
            yield scrapy.Request(
                url=myurl,
                callback=self.parse
            )
        else:
        	print("模拟人人网登录失败！")

    def parse(self, response):
        '''解析人人网个人主页信息'''
        print("正在访问人人网个人主页……")
        #提取人人网个人主页title信息
        print("个人主页标题："+response.xpath("//head").re_first("<title>(.*?)</title>"))
        print("完成人人网个人主页访问！")